//
//  ANOptionsTableViewController.m
//  Ants
//
//  Created by ZHM on 16/7/1.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANOptionsTableViewController.h"
#import "ANOptionFootView.h"
#import "ANOptionsTableViewCell.h"

@interface ANOptionsTableViewController ()

@end

@implementation ANOptionsTableViewController

- (NSMutableArray *)arrayCells {
    if (!_arrayCells) {
        _arrayCells = [NSMutableArray array];
    }
    return _arrayCells;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayCells.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ANOptionsTableViewCell *cell = [ANOptionsTableViewCell cellWithTableView:self.tableView];

    cell.textLabel.text = self.arrayCells[indexPath.row];
    
    //当前选中CELL背景颜色 字体颜色
    if (indexPath.row == _indexRom) {
        cell.underline.alpha = 1;
        cell.textLabel.textColor = [UIColor orangeColor];
        switch (_CellBackgroundColor) {
            case ProvincesBackground:
                cell.backgroundColor = CitysBackgroundColor;
                break;
            case CitysBackground:
                cell.backgroundColor = AreaBackgroundColor;
                break;
            case AreaBackground:
                cell.backgroundColor = [UIColor whiteColor];
                break;
        }
    }else {
        //cell背景颜色
        switch (_CellBackgroundColor) {
            case ProvincesBackground:
                cell.backgroundColor = ProvincesBackgroundColor;
                break;
            case CitysBackground:
                cell.backgroundColor = CitysBackgroundColor;
                break;
            case AreaBackground:
                cell.backgroundColor = AreaBackgroundColor;
                break;
        }
        cell.underline.alpha = 0;
        cell.textLabel.textColor = [UIColor blackColor];
    }
    return cell;
}

#pragma mark -点击CELL
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *content = _arrayCells[indexPath.row];
    NSUInteger beforeRom = _indexRom;
    _indexRom = indexPath.row;

    //刷新恢复CELL颜色
    NSIndexPath *beforeIndexPath = [NSIndexPath indexPathForRow:beforeRom inSection:0];
    NSArray *indexArray=[NSArray arrayWithObject:beforeIndexPath];
    [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
    
    
    if ([self.delegate respondsToSelector:@selector(opitonsTableView:didSelectCellContent:rom:)]) {
        [self.delegate opitonsTableView:self didSelectCellContent:content rom:indexPath.row];
    }
}
#pragma mark -cell 删除按钮
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath :(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除服务器上的数据
        if ([self.delegate respondsToSelector:@selector(opitonsTableView:deleCellforRow:completion:)]) {
            [self.delegate opitonsTableView:self deleCellforRow:indexPath.row completion:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    
                    [SVProgressHUD showSuccessWithStatus:@"删除成功!"];
                    [self.tableView reloadData];
                }else {
                    [SVProgressHUD showSuccessWithStatus:@"删除失败!"];
                    [self.tableView reloadData];
                }
            }];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return OptionsTableViewCellHeight;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return NO;
    }
    return self.IsEditRow;
}



@end
